package com.sharer.last.controller;

import com.sharer.last.base.constant.CommonConstant;
import com.sharer.last.base.model.PageVO;
import com.sharer.last.base.model.R;
import com.sharer.last.interfaces.ValidateGroup;
import com.sharer.last.model.dto.moment.MomentChangeDTO;
import com.sharer.last.model.dto.moment.MomentQueryDTO;
import com.sharer.last.model.vo.moment.MomentVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sharer.last.annotation.OperationLogger;
import com.sharer.last.service.MomentService;

import javax.annotation.Resource;

/**
 * 博客动态后台管理
 *
 * @ClassName MomentAdminController
 * @Author wangjin
 * @Date 2023/5/5 21:04
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping(CommonConstant.BLOG_ADMIN_API + "/moment")
public class MomentAdminController {
    @Resource
    MomentService momentService;

    /**
     * 根据id查询动态
     *
     * @param id 动态id
     * @return
     */
    @GetMapping("/findById/{id}")
    public R<MomentVO> findById(@PathVariable Long id) {
        return R.success(momentService.findById(id));
    }

    /**
     * 分页查询动态列表
     *
     * @param queryDTO
     * @return
     */
    @PostMapping("/page")
    public R<PageVO<MomentVO>> page(@RequestBody MomentQueryDTO queryDTO) {
        PageVO<MomentVO> pageList = momentService.selectListByPage(queryDTO);
        return R.success(pageList);
    }

    /**
     * 发布动态
     *
     * @param dto 动态实体
     * @return
     */
    @OperationLogger("发布动态")
    @PostMapping("/create")
    public R create(@RequestBody @Validated(ValidateGroup.InsertGroup.class) MomentChangeDTO dto) {
        momentService.createMoment(dto);
        return R.handle();
    }

    /**
     * 更新动态
     *
     * @param dto 动态实体
     * @return
     */
    @OperationLogger("更新动态")
    @PutMapping("/update")
    public R update(@RequestBody @Validated(ValidateGroup.UpdateGroup.class) MomentChangeDTO dto) {
        momentService.updateMoment(dto);
        return R.handle();
    }

    /**
     * 更新动态公开状态
     *
     * @return
     */
    @OperationLogger("更新动态公开状态")
    @PutMapping("/published")
    public R updatePublished(@RequestBody MomentChangeDTO momentChangeDTO) {
        momentService.updatePublished(momentChangeDTO.getId(), momentChangeDTO.getIsPublished());
        return R.handle();
    }

    /**
     * 删除动态
     *
     * @param id 动态id
     * @return
     */
    @OperationLogger("删除动态")
    @DeleteMapping("/delete")
    public R delete(@RequestParam Long id) {
        momentService.removeMomentById(id);
        return R.handle();
    }
}
